//
//  myBill.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/5/22.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "myBill.h"
#import "headCelll.h"
#import "contentCell.h"
#import "returnMoney.h"
#import "billModel.h"
@interface myBill ()<UITableViewDelegate, UITableViewDataSource> {
    billModel *model;
}
@property (nonatomic, copy) NSMutableArray *listData;
@property (nonatomic, strong) UITableView *listView;

@end

@implementation myBill

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UIView *grayLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
    grayLine.backgroundColor = [UIColor mainColor];
    [self.view addSubview:grayLine];
    
    CGRect frame = CGRectMake(0, 1, kScreenWidth, kScreenHeight - StatusBarAndNavigationBarHeight - TabbarSafeBottomMargin - 46 - 35);
    self.listView = [[UITableView alloc]initWithFrame:frame];
    if (@available(iOS 11.0, *)) {
        self.listView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        //        if (iPhoneX) {
        //            self.detialView.contentInset = UIEdgeInsetsMake(0, 0, 88, 0);
        //        }else {
        //            self.detialView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
        //        }
        //        self.detialView.scrollIndicatorInsets = self.detialView.contentInset;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.listView registerNib:[UINib nibWithNibName:NSStringFromClass([headCelll class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([headCelll class])];
    [self.listView registerNib:[UINib nibWithNibName:NSStringFromClass([contentCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([contentCell class])];
    self.listView.delegate = self;
    self.listView.dataSource = self;
    self.listView.tableFooterView = [UIView new];
    [self.view addSubview:self.listView];
    
    UIButton *bottomView = [UIButton new];
    [self.view addSubview:bottomView];
    bottomView.backgroundColor = [UIColor custom_color:@"#A91E23"];
    if ([self.title isEqualToString:@"已出账"]) {
         [bottomView setTitle:@"立即还款" forState:0];
    }else {
         [bottomView setTitle:@"提前还款" forState:0];
    }
   
    [bottomView setTitleColor:[UIColor whiteColor] forState:0];
    [bottomView addTarget:self action:@selector(ImmediateRepaymentclick) forControlEvents:UIControlEventTouchUpInside];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.listView.mas_bottom);
        make.height.equalTo(@45);
    }];
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self billRequest];
}
- (void)ImmediateRepaymentclick {//立即还款
    returnMoney *VC = [returnMoney new];
    VC.money = model.shouldRepayment;
    if ([self.title isEqualToString:@"已出账"]) {
        VC.orderType = @"";
    }else {
        VC.orderType = @"1";
    }
    NSString *idd = @"";
    for (bill *modell in model.data) {
        idd =  [idd stringByAppendingString:modell.id];
        idd = [idd stringByAppendingString:@","];
        VC.oid = modell.oid;
    }
    VC.ID = [idd substringToIndex:[idd length] - 1];
    [self.navigationController pushViewController:VC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark --- uitableViewDelegate and dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (!model) {
        return 0;
    }
    return model.data.count + 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 169;
    }
    return 59;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        headCelll *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([headCelll class])];
        cell.model = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    contentCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([contentCell class])];
    cell.model = [model.data objectAtIndex:indexPath.row - 1];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *grayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    grayView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    return grayView;
}
#pragma mark --- 我的账单 请求数据
- (void)billRequest {
    NSString *FLG;
    if ([self.title isEqualToString:@"已出账"]) {
        FLG = @"";
    }else {
        FLG  = @"1";
    }
    NSDictionary *dic = @{ @"token":USERTOKEN,
                           @"flg" : FLG
                           };
    __weak typeof(self) weakSelf = self;
    NSString *url = [NSString stringWithFormat:@"%@", [[apiManger shareApiManger] myBill]];
    [networkManger PostHttpDataWithUrlStr:url Dic:dic SuccessBlock:^(id responseObject) {
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            model = [billModel mj_objectWithKeyValues:responseObject];
            if (model.data.count == 0) {
                UIView *v = [[[NSBundle mainBundle] loadNibNamed:@"NoRecordView" owner:self options:nil] lastObject];
                v.frame = self.view.bounds;
                [self.view addSubview:v];
            }else {
                [weakSelf.listView reloadData];
            }
        }else {
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
#pragma mark --- getter
- (NSMutableArray *)listData {
    if (!_listData) {
        _listData = [NSMutableArray array];
    }
    return _listData;
}

@end
